package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetTopicList;
import com.rjwh.dingdong.client.bean.localbean.CommunityPostListBean;
import com.rjwh.dingdong.client.bean.localbean.GetTopicData;
import com.rjwh.dingdong.client.bean.localbean.TopicData;
import com.rjwh.dingdong.client.bean.localbean.TopicHeader;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.CommunityPostModel;
import com.rjwh.dingdong.client.mvp.presenter.ICommunityPostPresenter;
import com.rjwh.dingdong.client.mvp.view.CommunityPostView;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wby on 2016/6/2.
 */
public class CommunityPostPresenter extends BasePresenter<CommunityPostView> implements ICommunityPostPresenter {
    private CommunityPostModel model;
    private SwipyRefreshLayoutDirection mDirection;

    public CommunityPostPresenter(Context context, CommunityPostView mMvpView) {
        super(context, mMvpView);
        model = new CommunityPostModel(this);
    }

    @Override
    public void doGetTopicList(String modularid, int pageNumber, String filter, boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        mDirection = direction;
        model.doGetTopicList(modularid, pageNumber, filter, isRefresh);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            switch (businessType) {
                case NetConstant.COMMUNITY_GETTOPICLIST:
                    getMvpView().setRefreshing(false);
                    ResGetTopicList getTopicList = (ResGetTopicList) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && getTopicList != null) {
                        GetTopicData topicData = getTopicList.getData();
                        if (topicData != null) {
                            List<TopicHeader> headerList = topicData.getHeader();
                            List<TopicData> topiclist = topicData.getTopiclist();
                            List<CommunityPostListBean> list = new ArrayList<>();
                            if (headerList != null) {
                                for (TopicHeader data : headerList) {
                                    list.add(new CommunityPostListBean(0, data));
                                }
                            }
                            if (topiclist != null) {
                                if (topiclist.isEmpty()) {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                                    if (mDirection == SwipyRefreshLayoutDirection.BOTTOM) {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data_more));
                                    } else {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data));
                                    }
                                } else {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.BOTH);
                                }
                                for (TopicData data : topiclist) {
                                    list.add(new CommunityPostListBean(1, data));
                                }
                            }
                            getMvpView().loadData(isRefresh, list);

                        }
                    } else {
                        getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                        ToastUtil.showToast(getContext(),getContext().getString(R.string.refresh_error));
                    }
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

}
